package org.stone.project.tx.config;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.stone.project.tx.handle.MsgSendUserTypeHandler;
import org.stone.project.tx.handle.MsgTypeHandler;
import org.stone.project.tx.sysEnum.ReceiveType;
import org.stone.project.tx.sysEnum.TxType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class TxConfig {
    /**
     * 消息类别处理器
     */
    @Autowired
    private List<MsgTypeHandler> msgTypeHandlerList;

    /**
     * 消息发送处理器
     */
    @Autowired
    private List<MsgSendUserTypeHandler> msgSendUserTypeHandlerList;



    @Bean(name = "typeMsgHandlerMap")
    public Map<TxType, MsgTypeHandler> typeMsgHandlerMap(){
        HashMap<TxType, MsgTypeHandler> res = new HashMap<TxType, MsgTypeHandler>();
        for (TxType value : TxType.values()) {
            MsgTypeHandler msgTypeHandler = finfdMsgHandler(value);
            if (ObjectUtil.isNotNull(msgTypeHandler)){
                res.put(value, msgTypeHandler);
            }
        }
        return res;
    }

    @Bean(name = "sendMsgHandlerMap")
    public Map<ReceiveType,MsgSendUserTypeHandler> sendMsgHandlerMap(){
        HashMap<ReceiveType, MsgSendUserTypeHandler> res = new HashMap<ReceiveType, MsgSendUserTypeHandler>();
        for (ReceiveType value : ReceiveType.values()) {
            MsgSendUserTypeHandler msgSendUserTypeHandler = findReiceiveMsgHandler(value);
            if (ObjectUtil.isNotNull(msgSendUserTypeHandler)){
                res.put(value, msgSendUserTypeHandler);
            }
        }
        return res;
    }





    public MsgTypeHandler finfdMsgHandler(TxType txType){
        List<MsgTypeHandler> res = msgTypeHandlerList.stream()
                .filter(e -> {
                    if (txType == e.getTxType()) {
                        return true;
                    }
                    return false;
                })
                .collect(Collectors.toList());
        if (res.size() > 1)
            throw new IllegalArgumentException(txType.describ+"存在多个处理器");
        else if (res.size() == 1){
            return res.get(0);
        }else
            return null;
    }


    public MsgSendUserTypeHandler findReiceiveMsgHandler(ReceiveType receiveType){
        List<MsgSendUserTypeHandler> res = msgSendUserTypeHandlerList.stream()
                .filter(e -> {
                    if (receiveType == e.getReceiveType()) {
                        return true;
                    }
                    return false;
                })
                .collect(Collectors.toList());
        if (res.size() > 1)
            throw new IllegalArgumentException(receiveType.describ+"存在多个处理器");
        else if (res.size() == 1){
            return res.get(0);
        }else
            return null;
    }
}
